package net.mingsoft.basic.aop;

import org.springframework.stereotype.*;
import org.aspectj.lang.*;
import java.util.*;
import net.mingsoft.basic.util.*;
import net.mingsoft.basic.entity.*;
import org.aspectj.lang.annotation.*;
import cn.hutool.core.util.*;
import java.lang.reflect.*;

@Component
@Aspect
public class SaveOrUpdateAop extends BaseAop
{
    @Pointcut("execution(* net.mingsoft..*Action.save(..))")
    public void save() {
    }
    
    @Pointcut("execution(* net.mingsoft..*Action.update(..))")
    public void update() {
    }
    
    @Before("save()")
    public void save(final JoinPoint jp) {
        this.setField(jp, "createDate", new Date());
        this.setField(jp, "updateDate", new Date());
        final ManagerEntity manager = BasicUtil.getManager();
        if (manager != null) {
            this.setField(jp, "createBy", manager.getId());
        }
    }
    
    @Before("update()")
    public void update(final JoinPoint jp) {
        this.setField(jp, "updateDate", new Date());
        final ManagerEntity manager = BasicUtil.getManager();
        if (manager != null) {
            this.setField(jp, "updateBy", manager.getId());
        }
    }
    
    private void setField(final JoinPoint jp, final String name, final Object obj) {
        try {
            final Object[] objs = jp.getArgs();
            if (objs.length == 0 || ObjectUtil.isNull(objs[0])) {
                return;
            }
            final Field[] allFields2;
            final Field[] allFields = allFields2 = BasicUtil.getAllFields(objs[0]);
            for (final Field field : allFields2) {
                if (name.equals(field.getName())) {
                    field.setAccessible(true);
                    field.set(objs[0], obj);
                }
            }
        }
        catch (Exception e) {
            this.LOG.error("Aop\u9519\u8bef\uff1a", (Throwable)e);
        }
    }
}
